use strict;

chdir 'XS';

my $cmd = qq(xsubpp -hiertype -csuffix .cpp PlPN.xs > PlPN.cpp);

my $cpp = 'PlPN.cpp';
my @xs = qw(
	PlPN.xs
	PN.xs
	Document.xs
	ScriptRegistry.xs
	TextOutput.xs
	SearchOptions.xs
	Options.xs
	AppEventSink.xs
	DocumentEventSink.xs
	TextEditorEventSink.xs
	ScriptRunner.xs
	Menu.xs
	Recorder.xs
	constants.xs
	typemap
	perl_utils.cpp
);

my $xs_newer = 0;
my $cpptime = -M $cpp;
for my $xs (@xs) {
	my $xstime = -M $xs;
	# remember, -M is script start minus file modification
	# so a smaller result from -M means a newer file
	if ($xstime < $cpptime) {
		$xs_newer = 1;
		last;
	}
}

$xs_newer = 1;

if ($xs_newer) {
	print "Generating CPP from XS via xsubpp\n";
	system $cmd;
}
else {
	print "Skipping xsubpp; no changes to XS since last time\n";
}
